package com.synergy.stockflow.procure.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.entity.SysUser;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.procure.entity.AcceptanceOnArrival;
import com.synergy.stockflow.procure.entity.InboundDetails;
import com.synergy.stockflow.procure.entity.PurchaseDetails;
import com.synergy.stockflow.procure.entity.PurchaseOrder;
import com.synergy.stockflow.procure.service.AcceptanceOnArrivalService;
import com.synergy.stockflow.procure.service.InboundDetailsService;
import com.synergy.stockflow.procure.service.PurchaseDetailsService;
import com.synergy.stockflow.procure.service.PurchaseOrderService;
import com.synergy.stockflow.sales.entity.Quotation;
import com.synergy.stockflow.service.SysUserService;
import com.synergy.stockflow.warehouse.entity.InventoryTransaction;
import com.synergy.stockflow.warehouse.service.InventoryTransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/acceptanceOnArrival")
public class AcceptanceOnArrivalController {

    @Autowired
    AcceptanceOnArrivalService acceptanceOnArrivalService;
    @Autowired
    InboundDetailsService inboundDetailsService;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    PurchaseOrderService purchaseOrderService;
    @Autowired
    PurchaseDetailsService purchaseDetailsService;
    @Autowired
    InventoryTransactionService inventoryTransactionService;
    @GetMapping
    public Result<Page<AcceptanceOnArrival>> getAllacceptanceOnArrival(
            @RequestParam(name = "acceptanceOnArrivalId", required = false) String acceptanceOnArrivalId,
            @RequestParam(name = "acceptanceOnArrivalPurchaseOrderId", required = false) String acceptanceOnArrivalPurchaseOrderId,
            @RequestParam(name = "acceptanceOnArrivalWarehouseId", required = false) String acceptanceOnArrivalWarehouseId,
            @RequestParam(name = "acceptanceOnArrivalInboundBatches", required = false) String acceptanceOnArrivalInboundBatches,
            @RequestParam(name = "acceptanceOnArrivalEmployeeId", required = false) String acceptanceOnArrivalEmployeeId,
            @RequestParam(name = "acceptanceOnArrivalEligibility", required = false) String acceptanceOnArrivalEligibility,
            @RequestParam(name = "acceptanceOnArrivalStorage", required = false) String acceptanceOnArrivalStorage,
            @RequestParam(name = "acceptanceOnArrivalInboundTime", required = false) String acceptanceOnArrivalInboundTime,
            @RequestParam(name = "acceptanceOnArrivalWarehousingClerk", required = false) String acceptanceOnArrivalWarehousingClerk
    ){
        Page<AcceptanceOnArrival> page = new Page<>(1, 10000);
        page.setRecords(acceptanceOnArrivalService.selectAll(page, acceptanceOnArrivalId,acceptanceOnArrivalPurchaseOrderId, acceptanceOnArrivalWarehouseId, acceptanceOnArrivalInboundBatches, acceptanceOnArrivalEmployeeId, acceptanceOnArrivalEligibility, acceptanceOnArrivalStorage, acceptanceOnArrivalInboundTime, acceptanceOnArrivalWarehousingClerk));
        for(AcceptanceOnArrival a:page.getRecords()){
            LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
            a.setNickname(sysUserService.getOne(queryWrapper.eq(SysUser::getId,a.getAcceptanceOnArrivalEmployeeId())).getNickname());
            queryWrapper=new LambdaQueryWrapper<>();
            a.setNickname1(sysUserService.getOne(queryWrapper.eq(SysUser::getId,a.getAcceptanceOnArrivalWarehousingClerk())).getNickname());
        }
        return Result.success(page);
    }

    @PostMapping
    public Result<AcceptanceOnArrival> insert(@RequestBody AcceptanceOnArrival acceptanceOnArrival){
        acceptanceOnArrivalService.save(acceptanceOnArrival);
        List<AcceptanceOnArrival> list = acceptanceOnArrivalService.list();
        for(InboundDetails i:acceptanceOnArrival.getInboundDetailsList()){
            if(i.getInboundDetailsQuantityOfArrival().doubleValue()!=0.00 && i.getInboundDetailsQuantityOfArrival()!=null){
                i.setInboundDetailsArrivalId(list.get(list.size()-1).getAcceptanceOnArrivalId());
                i.setInboundDetailsId("YSD-"+String.valueOf(new Date().getTime()));
                inboundDetailsService.save(i);

                String warehouseId =  list.get(list.size()-1).getAcceptanceOnArrivalWarehouseId();
                String sourceDocId=list.get(list.size()-1).getAcceptanceOnArrivalId();
                double v=i.getInboundDetailsInboundQuantity().doubleValue();
                String materialId=purchaseDetailsService.selectWa(i.getInboundDetailsPurchaseDetailsId()).getPurchaseDetailsProductsId();
                InventoryTransaction inventoryTransaction = new InventoryTransaction(null,"PO-IN",sourceDocId,"acceptance_on_arrival",warehouseId,materialId,i.getInboundDetailsId(),new BigDecimal(v),new Date(),"1","验收入库",null,null);
                inventoryTransactionService.save(inventoryTransaction);
            }
        }

        chaxuntongji(acceptanceOnArrival);

        return Result.success(acceptanceOnArrival);
    }

    //查询是否完成采购订单
    public String chaxuntongji(AcceptanceOnArrival acceptanceOnArrival){
        //判断该采购订单是否有未入库订单
        List<AcceptanceOnArrival> ruku = acceptanceOnArrivalService.isRuku(acceptanceOnArrival.getAcceptanceOnArrivalPurchaseOrderId());

        for(int i =0;i<ruku.size();i++){
            if(ruku.get(i).getAcceptanceOnArrivalStorage().equals("否")){
                break;
            }else if(i==ruku.size()-1){
                PurchaseOrder purchaseOrders = purchaseOrderService.selectDetails(acceptanceOnArrival.getAcceptanceOnArrivalPurchaseOrderId());
                for (int j = 0;j<purchaseOrders.getPurchaseDetailsList().size();j++){
                    InboundDetails a = inboundDetailsService.tongji(purchaseOrders.getPurchaseDetailsList().get(j).getPurchaseDetailsId());
                    if(a.getInboundDetailsInboundQuantity()==null||a.getInboundDetailsInboundQuantity().doubleValue()!=a.getInboundDetailsInboundQuantity().doubleValue()){
                        break;
                    }else if(j==purchaseOrders.getPurchaseDetailsList().size()-1){
                        purchaseOrderService.updateState(acceptanceOnArrival.getAcceptanceOnArrivalPurchaseOrderId());
                    }
                }
            }
        }
        return "1";
    }
    @PutMapping
    public int update(@RequestBody AcceptanceOnArrival acceptanceOnArrival){
        acceptanceOnArrivalService.updateById(acceptanceOnArrival);
        chaxuntongji(acceptanceOnArrival);
        return 0;
    }
    @DeleteMapping
    public int delete(@RequestBody List<AcceptanceOnArrival> acceptanceOnArrivals){
        for(AcceptanceOnArrival a:acceptanceOnArrivals){
            acceptanceOnArrivalService.removeById(a.getAcceptanceOnArrivalId());
            inboundDetailsService.deleteinboundDetailsArrivalId(a.getAcceptanceOnArrivalId());
        }
        return 0;
    }

    @GetMapping("/selectT")
    public Result<List<AcceptanceOnArrival>> selectT(@RequestParam(name = "acceptanceOnArrivalPurchaseOrderId", required = false) String acceptanceOnArrivalPurchaseOrderId){
        List<AcceptanceOnArrival> acceptanceOnArrivals = acceptanceOnArrivalService.selectT(acceptanceOnArrivalPurchaseOrderId);
        return Result.success(acceptanceOnArrivals);
    }
}
